package Queue;

public class ArrQueue<T> {

	public static final int DEFAULT_SIZE = 10;

	private T[] queue;
	private int head = 0;
	private int end = 0;

	public ArrQueue() {
		this(DEFAULT_SIZE);
	}

	public ArrQueue(int size) {
		queue = (T[]) new Object[size];
		init();
	}

	public void init() {
		for (int i = 0; i < queue.length; i++) {
			queue[i] = null;
		}
		head = 0; // head is first position
		end = 0; // end is next element position
	}

	public int size() {
		if (end == head) {
			return ((queue[(head + 1) % queue.length] == null) || (queue[(head - 1)
					% queue.length] == null)) ? (0)
					: (queue.length);
		} else {
			return (end > head) ? (end - head) : (queue.length
					- head + end);
		}
	}

	public boolean add(T element) {
		if (size() >= queue.length) {
			System.err.println("lenth error");
			return false;
		} else {
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			queue[head] = element;
			end++;
			return true;
		}
	}

	public boolean remove() {
		return remove(size() - 1);
	}

	public boolean remove(int index) {
		if (index >= size() - 1) {
			System.err.println("No element");
			return false;
		} else {
			index = (head + index) % queue.length;
			queue[end] = null;
			if (end == 0) {
				end = queue.length - 1;
			} else {
				end--;
			}
			return true;
		}
	}

	public static void main(String[] args) {
		ArrQueue<Integer> a = new ArrQueue<Integer>(1);
	}
}
